package linked;

public class LinkedList1<E> {

    private Node dummyHead;
//    private Node head;
    private int size;

    public LinkedList1() {
        dummyHead = new Node(null,null);
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return 0 == size;
    }
    public void addFirst(E e) {
//        dummyHead.next = new Node(e, dummyHead.next);
//        size++;
        add(0, e);
    }

    public void addLast(E e){
        add(size,e);
    }

    public void add(int index, E e) {
        if (index < 0 && index > size) {
            throw new IllegalArgumentException("add failed,Illegal index.");
        }
//        Node prev = dummyHead.next;
//        for (int i = 0; i < index - 1; i++) {
//            prev = prev.next;
//        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size++;
    }

    public E get(int index){
        if (index<0 || index > size){
            throw new IllegalArgumentException("get failed,Illegal index.");
        }
//        Node prev = dummyHead;
//        for (int i = 0; i <= index; i++) {
//            prev = prev.next;
//        }
//        return prev.data;
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.data;
    }

    public E getFirst(){
        return get(0);
    }
    public E getLast(){
        return get(size - 1);
    }

    public void set(int index,E e){
        if (index<0 || index >= size){
            throw new IllegalArgumentException("set failed,Illegal index.");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.data = e;
    }

    public boolean contains(E e ){
        Node cur = dummyHead.next;
        while (cur!=null ){
            if (cur.data.equals(e)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public E remove(int index){
        if (index<0 || index > size){
            throw new IllegalArgumentException("delete failed,Illegal index.");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;
        size--;
        return delNode.data;
    }



    public E removeFirst(){
        return remove(0);
    }
    public E removeLast(){
        return remove(getSize() - 1);
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder("");
        String format = String.format("LinkedList1 size:%d head:", getSize());
        ret.append(format);
        Node tmp = dummyHead.next;
        while (tmp != null ) {
            ret.append(tmp);
            ret.append("--> ");
            tmp = tmp.next;
        }
        ret.append("null");
        return ret.toString();
    }

    private class Node {
        public E data;
        public Node next;

        public Node(E e, Node node) {
            data = e;
            next = node;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return data.toString();
        }
    }
}
